//
//  WSCloudModels.swift
//  PDFelement
//
//  Created by ws on 2022/4/28.
//  Copyright © 2022 Wondershare. All rights reserved.
// swiftlint:disable identifier_name

import Foundation
import Moya

public struct WSCloudResponse<T: Codable>: Codable {

    var msg: String = ""
    var status: Int = 0
    var result: T?

}

public struct WSCloudList: Codable {

    public var directory_list: [WSCloudModel]?
    public var file_list: [WSCloudModel]?

}

// MARK: - FileModel
public struct WSCloudModel: Codable {

    // 文件数据
    var file_id: String?
    var img_path: String?
    var file_name: String?
    var file_size: UInt64?
    var protected: Int?
    var owner_protected: Int?
    var md5_code: String?

    // 文件夹数据
    var did: Int?
    var name: String?
    var items_num: Int?

    // 共有数据
    var last_time: Int?
    var path: String?

}

/// 云端存储容量接口
public struct WSCloudCapacity: Codable {

    var totalCapacity: Int64 = 0
    var usedCapacity: Int64 = 0
    var leftCapacity: Int64 = 0

    enum CodingKeys: String, CodingKey {
        case totalCapacity = "total_capacity"
        case usedCapacity  = "used_capacity"
        case leftCapacity  = "left_capacity"
    }

}

/// 获取上传阿里云oss接口
public struct WSCloudUpload: Codable {

    var uploadURL: String = ""
    var ossCallback: String = ""

    enum CodingKeys: String, CodingKey {
        case uploadURL = "upload_url"
        case ossCallback = "callback"
    }

}

// Decodable Extension
extension Decodable {

    static func decode(data: Data) throws -> Self {
        let decoder = JSONDecoder()
        return try decoder.decode(Self.self, from: data)
    }

    static func decode(jsonString: String) throws -> Self {
        if let data = jsonString.data(using: .utf8) {
            return try Self.decode(data: data)
        }
        throw ResponseError.decodeError
    }

    static func map(_ response: Moya.Response) -> Self? {
        guard let json = try? response.mapString() else {
//            Log.error(response)
            return nil
        }
        do {
            return try Self.decode(jsonString: json)
        } catch {
            print(error)
        }
        return nil
    }

}

public enum ResponseError: Error {
    case decodeError
}
